#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/6/16 15:55
# @Author  : 胡浩浩
# @Email   : hu_haohao@venusgroup.com.cn
# @File    : get_data_value.py
# @IDE     : PyCharm
# @description : 获取元数据相关的信息
import json

import jmespath
import requests

from commons.common_tools import write_to_json_file
from commons.user_login import UserLogin


def get_mate_data_value():
    host = f"https://192.168.114.38:8443/csa"
    username = "sysadmin"
    password = "Sa_7654321"
    # password = "leadsec.sysadmin"
    # password = "123qwe!@#QWE"
    user = UserLogin(host, username, password)
    session, token = user.user_login()
    params = {
        'page': '1',
        'rows': '100',
        'sidx': '',
        'sord': '',
        'isbase': '',
        'fieldtype': '',
        'enumtype': '2',
        'isfilter': '',
        'isdisplay': '',
        'isenrich': '',
        'isorder': '',
        'ismerge': '',
        'name': '',
        'metadataType': 'metadata_log',
        'deptid': ''
    }
    response = session.get(host + "/api/v2/logfield/metadata/searchList", params=params, verify=False)

    data_json = {}
    data_enname_list = jmespath.search("data.data[].enname", response.json())
    for enname in data_enname_list:
        data = f'fieldenname={enname}&enumtype=2&superiorid='
        response = session.post(
            host + '/api/v2/metadata/dict/getDictListByEnname',
            headers={'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
            data=str(data).encode("utf-8"),
            verify=False)
        # print(response.text)
        key_value_list = jmespath.search("data[0].dictChild[].[dictcode,dictname]", response.json())
        data_json[enname] = {}
        if key_value_list:
            for item in key_value_list:
                data_json[enname][item[0]] = item[1]
    print(json.dumps(data_json, ensure_ascii=False))
    write_to_json_file("../data/data_value_520.json", data_json)


if __name__ == '__main__':
    get_mate_data_value()
